<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>汇率兑换计算器</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            max-width: 600px;
            margin: 50px auto;
            padding: 20px;
            background-color: #f0f2f5;
        }
        .calculator {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 15px rgba(0,0,0,0.1);
        }
        h1 {
            color: #1a73e8;
            text-align: center;
            margin-bottom: 30px;
        }
        .input-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 8px;
            color: #5f6368;
        }
        input, select {
            width: 100%;
            padding: 12px;
            border: 1px solid #dadce0;
            border-radius: 5px;
            font-size: 16px;
            margin-bottom: 10px;
        }
        button {
            background: #1a73e8;
            color: white;
            border: none;
            padding: 12px 24px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            transition: background 0.3s;
        }
        button:hover {
            background: #1557b0;
        }
        #result {
            margin-top: 25px;
            padding: 15px;
            background: #e8f0fe;
            border-radius: 5px;
            display: none;
        }
    </style>
</head>
<body>
    <div class="calculator">
        <h1>美元 ↔ 人民币汇率兑换</h1>
        <div class="input-group">
            <label for="amount">输入金额：</label>
            <input type="number" id="amount" placeholder="请输入金额">
        </div>
        
        <div class="input-group">
            <label for="currency">选择货币类型：</label>
            <select id="currency">
                <option value="USD">美元 (USD)</option>
                <option value="CNY">人民币 (CNY)</option>
            </select>
        </div>

        <button onclick="convert()">立即转换</button>
        <button onclick="clearFields()" style="margin-left: 10px; background: #5f6368">清除</button>

        <div id="result"></div>
    </div>

    <script>
        const EXCHANGE_RATE = 7.0; // 固定汇率

        function convert() {
            const amountInput = document.getElementById('amount');
            const currencySelect = document.getElementById('currency');
            const resultDiv = document.getElementById('result');

            const amount = parseFloat(amountInput.value);
            const currency = currencySelect.value;

            if (isNaN(amount) || amount <= 0) {
                showResult('请输入有效的正数金额', 'error');
                return;
            }

            let conversion;
            if (currency === 'USD') {
                conversion = amount * EXCHANGE_RATE;
                showResult(`${amount} 美元 = ${conversion.toFixed(2)} 人民币`);
            } else {
                conversion = amount / EXCHANGE_RATE;
                showResult(`${amount} 人民币 = ${conversion.toFixed(2)} 美元`);
            }
        }

        function showResult(message, type = 'success') {
            const resultDiv = document.getElementById('result');
            resultDiv.style.display = 'block';
            resultDiv.style.color = type === 'error' ? '#dc3545' : '#28a745';
            resultDiv.textContent = message;
        }

        function clearFields() {
            document.getElementById('amount').value = '';
            document.getElementById('currency').selectedIndex = 0;
            document.getElementById('result').style.display = 'none';
        }
    </script>
</body>
</html>
